PHP 5 Social Networking
Total Page:16
File Type:pdf, Size:1020Kb
PHP 5 Social Networking Create a powerful and dynamic social networking website in PHP by building a flexible framework Michael Peacock & FPAf ICTl °Pen source I I llv IV I I community experience distilled PUBLISHING BIRMINGHAM - MUMBAI Table of Contents Preface 1 Chapter 1: PHP Social Networking 7 Introduction to social networks 7 Business logic to social networks 8 Examples: Businesses making use of existing social networks and their own social networks 9 Existing social networks 10 Facebook 10 Linkedln 10 MySpace 11 Twitter 11 Existing social networking software 12 Drupal 12 Elgg 12 Joomla! 12 Hybrid approaches 12 Rolling your own 13 Why roll your own? 13 Easier to update and maintain 14 Licensing 14 Enhance knowledge 14 Provide a service 14 Improve business 15 Improve communication 15 Why use PHP? 16 When to use something else 16 Our site: DinoSpace 16 Feature list 18 Limitations 19 Summary 20 Table of Contents Chapter 2: Planning and Developing the Core Framework 21 Designing the framework 22 Patterns—making life easier 22 MVC: Model-View-Controller 22 The Front Controller pattern 24 Registry 24 Folder structure 26 Building the framework 28 Registry 28 The registry object 28 Registry objects 31 Front Controller: single point of access 56 index.php 56 .htaccess 58 Summary 59 Chapter 3: Users, Registration, and Authentication 61 Privacy policies 62 Users 63 Our user object 63 Our authentication registry object 65 POST authentication 67 SESSION authentication 68 Structuring the database 69 Registration 70 Standard details 70 Hooking additional fields on 76 Processing the registration 80 Creating the profile 80 Putting it all together: registration constructor 81 CAPTCHA 82 General CAPTCHA 83 reCAPTCHA 83 Where do I sign up? 83 E-mail verification 86 Sending e-mails 86 Sending the e-mail verification e-mail 90 Authentication with our authentication object 90 Logging in 90 Are we logged in? 91 Logging out 91 Remember me 92 Help! I've forgotten! 92 Usemame 92 Table of Contents Password 94 Let them reset the password 96 Summary 98 Chapter 4: Friends and Relationships 99 Inviting friends 99 Manually inviting friends 100 Invitation controller 101 Automatically inviting friends 101 Google Friend Connect 101 Windows Live contacts 102 Yahoo! 102 Gmail contacts 102 Automatically connecting with friends 102 Members 102 Listing users 103 Pagination 103 Paginated members 110 Paginated users by letter 113 Searching for users 117 Custom relationships 121 Relationship types 121 Relationships 122 Adding friends 122 Forming a relationship 122 Relationship model 125 Relationship controller 129 Mutual relationships—accepting or rejecting a request 131 Pending requests 131 Accepting a pending request 133 Rejecting a pending request 134 Listing friends 134 Our friends 134 Their friends 136 Mutual friends 136 Friends in your profile 137 Summary 137 Chapter 5: Profiles and Statuses 139 User profiles 139 Extendable profile 140 Profile controller 140 Core shared information 142 Static profile 151 Viewing the profile 151 [Hi] Table of Contents Relationships—some improvements 155 Editing the profile 157 Statuses 170 Statuses database table 170 Statuses types database table 170 Different types of status 171 Template improvements 171 Listing statuses 173 Templates 175 In action 176 Likes, dislikes, and comments 176 Comments 176 Summary 180 Chapter 6: Status Stream 181 What is a status stream? 181 Stream model 182 Building the stream 182 Relationships—get the IDs! 184 Friendly times 185 The rest... 188 Stream controller 189 Generating the stream 190 Comments, likes, and dislikes 193 Comments 193 Likes and dislikes 194 Views 195 Main template 195 Status type templates 196 Inaction 196 Room for improvement 196 A system stream for administrators 197 Summary 198 Chapter 7: Public and Private Messages 199 Public messages 199 Controller 199 Displaying profile messages 200 Displaying the post message box 201 Displaying a confirmation message 207 View 208 In action 209 Private messages 210 Database 210 Table of Contents Message model 211 Messages model 217 Controllers and views 218 Listing messages 219 Reading a message 220 Deleting a message 223 Composing a new message 224 Creating a message template 227 In action 228 Room for improvement? 228 Sent items 228 Replies 229 Group messages 229 Summary 229 Chapter 8: Statuses—Other Media 231 Why support other media types? 231 Changes to the view 232 Template 232 jQuery to enhance the user experience 233 View in action 234 Images 234 Database table 234 Model 235 Class, variable, and constructor 235 Processing the image upload 236 Saving the status 244 Video (via YouTube) 244 Database 244 Model 245 Links 246 Database 246 Model 247 Extending the profiles 248 Processing the new status posts 249 Altering our profile status' query 250 Status views 250 Images 250 Video 250 Links 251 In action 251 Images 251 Videos 252 Links 252 [v] Table of Contents Repeat! 252 Summary 253 Chapter 9: Events and Birthdays 255 Let's plan 255 Calendars: what do we need to be able to do? 256 Calendar library 256 Generating the month 259 Days in the month 262 Ordered days 264 Previous month 264 Next month 265 Displaying a calendar 265 Generate and output 266 Multiple calendars 271 With events 272 Birthdays 272 Getting relationship IDs 273 Setting up the calendar 273 Getting the birthdays 275 Passing them to the calendar 275 The results 276 Events 277 Event model 277 Events model 284 Attendees, invitations, and RSVPs 288 RSVPs 290 Controller 290 Creating an event 290 Calendar of events 294 Viewing an event 294 Upcoming events 297 Reminders 298 On-site notifications 298 E-mail notifications 298 SMS notifications 298 Summary 299 Chapter 10: Groups 301 Some planning 301 Group information 302 Types of groups 302 Ownership 303 Table of Contents Membership 303 Features 303 A group 303 Discussion 303 Database 304 Post 305 Topic 308 The group itself 315 Group table 315 Model 315 Creating a group 321 Controller 321 View 323 Creating a group—in action 324 Viewing a group 324 Membership 324 Controller 331 View 334 In action 335 Discussing within a group 335 Group controller additions 336 View 338 Discussion in action—viewing a topic 340 Joining a group 340 Joining (public) groups 340 Groups 342 Listing groups 342 Group controller addition 342 Template 344 In action 344 My groups 345 Addition to the group's controller 345 Template file 345 In action 346 Summary 346 Chapter 11: Developing an API 347 What is an API and why should we create one? 347 APIs in social networks 348 Facebook 348 MySpace 348 OpenSocial 349 Some planning 349 What should it do, and who should be able to do what? 349 How should it work? 350 How could it work? 351 [vii] Table of Contents Let's go with REST 352 Further reading 354 Implementation 354 Data format 354 API controller 355 Wait—no models? 359 Authentication 359 Delegating control: API controllers for our features 363 Profile's delegate 363 An Application Framework API 368 One solution; use OpenSocial 369 Consuming 369 POSTing data to our API with cURL 371 Summary 371 Chapter 12: Deployment, Security, and Maintenance 373 Deploying the site 373 Choosing a domain name 374 Registering a domain name 374 Popular domain name registrars 374 Signing up with a hosting provider 375 Choosing a web hosting provider 375 Considerations for hosts of social networking websites 377 Popular web hosting providers 377 Setting the nameservers for the domain 378 Creating a database on the hosting account 378 With cPanel hosting control panel 378 With appropriate privileges on phpMyAdmin 380 Exporting our local database 381 Importing our local database to the hosting account 382 Changing some of our database records 383 Changing our database configuration options 384 Uploading the files 384 Testing 385 Automating deployment 385 Security 386 Server Security 387 Software 387 Securing the site with a firewall 388 Shared hosting precautions 388 Passwords 388 Error reporting 389 Directory listings 390 SPAM 390 Table of Contents Maintenance 390 Backing up and restoring your social network 390 With cPanel 391 Using the command line 392 Do they work? 393 Access logs and statistics 393 Summary 394 Chapter 13: Marketing, SEO, User Retention, and Monetization Strategies 395 Marketing 396 Online advertising 396 Pay-Per-Click 396 Advertising space 398 Newsletter advertising 400 Newsletters 401 Social marketing 401 Viral marketing campaigns 402 Twitter 402 RSS feeds 402 Search engine optimization 403 On-site SEO 403 Headings 403 Links 404 Up to date, relevant content 404 Page metadata 404 Site speed 405 Search engine goodies—sitemaps and tools 405 Off-site SEO 406 What to look for in an SEO company 407 User retention 407 E-mails for the user's action 407 User feedback 408 Hello there! 408 Monetization options 408 Final tips: web stats 408 Summary 409 Chapter 14: Planning for Growth 411_ Code performance 412 Code profiling 412 Slow queries 412 Compression 413 Useful tools and resources 413 Table of Contents Server performance 414 Apache 414 MySQL 415 Alternative web servers 415 Scaling 415 VPS Cloud Hosting 415 Additional servers 416 Caching systems 416 Memcached 417 Available caching systems 417 Redundancy 417 Content Delivery Networks 418 Message queues 419 Message queue versus database table 419 What can we queue? 419 Processing queued tasks 419 No SQL 420 Learn from the experts 420 Farm it out 421 Summary 421 Index 423 [X].